/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package udio;
/*
 * UdioLogin.java
 *
 * Created on Nov 19, 2011, 4:29:55 PM
 */
/**
 *
 * @author jmelai
 */
import java.sql.*;

public class UdioLogin extends javax.swing.JFrame {

    /** Creates new form UdioLogin */
    public UdioLogin() {
        initComponents();
    }
    
    public static int checkUser(String login_username, char[] clogin_password) {
        String username;
        String password;
        ResultSet results;
        String selectQuery;
        int userType;
        
        String login_password = String.valueOf(clogin_password);
        try
        {
            //create driver - ALTER TO SUIT YOUR DRIVER/ DBMS
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            //database connection code - ENTER YOUR DETAILS
            username = "seng330";
            password = "fightingchickensco";

            //URL - ALTER TO CONNECT TO YOUR DATABASE
            String dbURL = "jdbc:mysql://sql09.freemysql.net/seng330project?user="
                    + username + "&password=" + password;

            //create the connection - ALTER FOR YOUR DBMS
            java.sql.Connection myConnection =
                    DriverManager.getConnection(dbURL);

            //create statement handle for executing queries
            Statement stat = myConnection.createStatement();

            System.out.println("login_username = " + login_username);
            System.out.println("login_password = " + login_password);

            //query to check if user's authorization
            selectQuery = "Select * from UserDB where username='" + login_username + "'";
             //get the results
            results = stat.executeQuery(selectQuery);

            if (results.next())
            {
                if (login_password.equals(results.getString("password"))) {
                    userType = results.getInt("userType");
                    return userType;
                } else {
                    userType = -1;
                    return userType;
                }
            }
            else
            {
                userType = -1;
                return userType;
            }
        }
        catch( Exception E )
        { 
            System.out.println( E.getMessage());
        }
        return 0;
    }
    
    public static int getCustomerID() {

        int custID = 0;
        DB db = new DB();
        Connection conn = db.connect();
        String query = "SELECT customerId FROM userDB WHERE login_username = '" + testme + "'";
        ResultSet result = db.getQuery(conn,query);
        db.disconnect(conn);

        try{
            custID = result.getInt("customerId");
        } catch(Exception e) { }

        return custID;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        errorLogin = new javax.swing.JLabel();
        jPasswordField2 = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Sign in");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setIcon(null);

        jLabel4.setText("Username:");

        jLabel5.setText("Password:");

        errorLogin.setForeground(new java.awt.Color(204, 0, 0));
        errorLogin.setText("Incorrect login credentials!");

        org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(jLabel2)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel3))
                            .add(jLabel1)))
                    .add(mainPanelLayout.createSequentialGroup()
                        .add(74, 74, 74)
                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(jLabel4)
                                    .add(jLabel5))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                    .add(jPasswordField2)
                                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 150, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(jButton1)))
                    .add(mainPanelLayout.createSequentialGroup()
                        .add(94, 94, 94)
                        .add(errorLogin)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2)
                    .add(mainPanelLayout.createSequentialGroup()
                        .add(jLabel1)
                        .add(48, 48, 48)
                        .add(jLabel3)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jPasswordField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jButton1)
                .add(18, 18, 18)
                .add(errorLogin)
                .addContainerGap(42, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(0, 6, Short.MAX_VALUE)
                    .add(mainPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(0, 6, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 424, Short.MAX_VALUE)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(0, 0, Short.MAX_VALUE)
                    .add(mainPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(0, 0, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
private int userFlag;
public static String testme;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    
    UdioCustomer customerForm = new UdioCustomer();
    UdioDepotStaff depotForm = new UdioDepotStaff();

    String login_username = jTextField1.getText();
    testme = login_username;
    char[] login_password = new char[20];
    login_password = jPasswordField2.getPassword();
    
    if (login_username.equals("")) {
        userFlag = -1;
    } else {
        userFlag = checkUser(login_username, login_password);
    }
    
    System.out.println("userFlag = " + userFlag);
    if (userFlag == 0) {      
        jTextField1.setText(null);
        jPasswordField2.setText(null);
        errorLogin.setVisible(false);
        customerForm.setVisible(true);
        dispose();
    } else if (userFlag == 1) {
        jTextField1.setText(null);
        jPasswordField2.setText(null);
        errorLogin.setVisible(false);
        depotForm.setVisible(true);
        dispose();
    } else if (userFlag == -1) {
        jTextField1.setText(null);
        jPasswordField2.setText(null);
        errorLogin.setVisible(true);
    }
}//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UdioLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UdioLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UdioLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UdioLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new UdioLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel errorLogin;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JPanel mainPanel;
    // End of variables declaration//GEN-END:variables
}
